package com.udf.hive.unit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    private static Pattern provincePattern = Pattern.compile(".*(省)");
    private static Pattern cityPattern = Pattern.compile(".*(市)");
    private static Pattern ipPattern = Pattern.compile("[0-9]{1,3}\\.][0-9]{1,3}\\.][0-9]{1,3}\\.][0-9]{1,3}");

    public static Boolean isIPMatch(String ip) {
        return StringUtils.isNullAndEmpty(ip)/* && ipPattern.matcher(ip).find()*/;
    }

    public static String parseProvince(String addrStr) {
        if (StringUtils.isNullAndEmpty(addrStr)) {
            Matcher provinceMatch = provincePattern.matcher(addrStr);
            if (provinceMatch.find()) {
                return provinceMatch.group(0);
            } else {
                Matcher cityMatch = cityPattern.matcher(addrStr);
                if (cityMatch.find()) {
                    return cityMatch.group();
                }
            }
        }
        return "";
    }

    public static String parseCity(String addrStr) {
        if (StringUtils.isNullAndEmpty(addrStr)) {
            Matcher provinceMatch = cityPattern.matcher(addrStr);
            if (provinceMatch.find()) {
                String[] provinceAndCity = provinceMatch.group(0).split("省");
                if (provinceAndCity.length > 1) {
                    return provinceAndCity[1];
                } else {
                    return provinceAndCity[0];
                }
            }
        }
        return "";
    }

    public static String parseCounty(String addrStr) {
        if (StringUtils.isNullAndEmpty(addrStr)) {
            Matcher provinceMatch = cityPattern.matcher(addrStr);
            if (provinceMatch.find()) {
                return addrStr.replace(provinceMatch.group(0), "");
            }
        }
        return "";
    }


    public static Boolean isNullAndEmpty(String input) {
        return input != null && !input.trim().isEmpty();
    }
}
